package com.one.consumer.order.presenter;

import android.content.Context;

import com.one.common.common.user.modle.UserInfoModel;
import com.one.common.model.extra.DefaultExtra;
import com.one.common.model.http.callback.ObserverOnResultListener;
import com.one.common.presenter.BaseApiPresenter;
import com.one.common.utils.MyToast;
import com.one.common.utils.StringUtils;
import com.one.common.utils.Toaster;
import com.one.common.utils.list.ListUtils;
import com.one.common.view.base.BaseActivity;
import com.one.common.view.base.IView;
import com.one.consumer.order.model.OrderModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/11/3 8:53 PM
 * description ： 添加评价
 **/
public class AddCommentPresenter extends BaseApiPresenter<IView, OrderModel> {

    private DefaultExtra extra;

    public AddCommentPresenter(IView mView, Context mContext) {
        super(mView, mContext, new OrderModel((BaseActivity) mContext));
    }

    @Override
    public void onCreate() {
        super.onCreate();
        extra = (DefaultExtra) mActivity.getIntent().getSerializableExtra(DefaultExtra.getExtraName());
    }

    private Map<String, String> params = new HashMap<>();

    /**
     * 添加评价
     */
    public void submit(String star1, String star2, String desc, ArrayList<String> imgs) {
        params.put("order_id", extra.getStr());
        params.put("assessment_star1", star1);
        params.put("assessment_star2", star2);
        if(StringUtils.isBlank(desc)){
            Toaster.showLongToast("请输入评价内容");
            return;
        } else {
            params.put("assessment_desc",desc);
        }

        if(ListUtils.isNotEmpty(imgs)){
            uploadImgs(imgs);
        } else {
            addComment();
        }

    }

    /**
     * 上传图片
     */
    public void uploadImgs(ArrayList<String> paths){
        new UserInfoModel(mActivity).uploadImgs(paths, new ObserverOnResultListener<ArrayList<String>>() {
            @Override
            public void onResult(ArrayList<String> s) {
                if(ListUtils.isNotEmpty(s)){
                    params.put("assessment_imgs", ListUtils.join(s));
                    addComment();
                }
            }
        });
    }

    /**
     * 添加评价
     */
    private void addComment() {
        mModel.evaluateOrder(params, new ObserverOnResultListener<Object>() {
            @Override
            public void onResult(Object o) {
                if (mView != null) {
                    Toaster.showLongToast("评价成功", MyToast.SUCCESS);
                    mView.finishPage();
                }
            }
        });


    }
}
